#define _CRT_SECURE_NO_WARNINGS 
#include"list.h"



ltnode* initltnode()
{
	ltnode* phead = creatltnode(-1);
	phead->next = phead;
	phead->prew = phead;

	return phead;
}

ltnode* creatltnode(LTdatetype x)
{
	ltnode* newnode = (ltnode*)malloc(sizeof(ltnode));
	assert(newnode);

	newnode->val = x;
	newnode->next = NULL;
	newnode->prew = NULL;

	return newnode;
}

void ltprint(ltnode*phead)
{
	ltnode* cur = phead->next;
	printf("ͷ<->");

	while (cur != phead)
	{
		printf("%d<->", cur->val);
		cur = cur->next;
	}
}

void ltpushback(ltnode* phead, LTdatetype x)
{
	assert(phead);

	ltnode* newnode = creatltnode(x);

	newnode->next = phead;
	phead->prew->next = newnode;
	newnode->prew = phead->prew;
	phead->prew = newnode;

}

void ltpushfront(ltnode* phead, LTdatetype x)
{
	ltnode* newnode = creatltnode(x);

	newnode->next = phead->next;
	phead->next->prew = newnode;
	newnode->prew = phead;
	phead->next = newnode;
}

void ltpopback(ltnode* phead)
{
	assert(phead);
	assert(phead->next != phead);

	ltnode* tail = phead->prew;
	phead->prew = tail->prew;
	tail->prew->next = phead;

	free(tail);
}

void ltpopfront(ltnode* phead)
{
	assert(phead);
	assert(phead->next != phead);

	ltnode* tail = phead->next;
	phead->next = tail->next;
	tail->next->prew = phead;

	free(tail);
}

ltnode* ltfind(ltnode*phead,LTdatetype x)
{
	assert(phead);

	ltnode* cur = phead->next;

	while (cur != phead)
	{
		if (cur->val == x)
			return cur;
		cur = cur->next;
	}
}